package edu.ucsd.cs110.mycity;

import edu.ucsd.cs110.mycity.controller.ViewController;
import edu.ucsd.cs110.mycity.controller.XMPPController;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/*
 * Login Activity
 */

@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnClickListener {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy); 

		super.onCreate(savedInstanceState);
		// Check if logged in 
		ViewController.setLoginScreen(this);
		
		if ( XMPPController.isLoggedIn() ) {
			setContentView(R.layout.activity_map);
		} else {
			setContentView(R.layout.activity_main);
			Button loginButton = (Button) findViewById(R.id.loginButton);
			loginButton.setOnClickListener(this); // Wait for login input
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	protected void onResume() {
		MyCity.resume();
	    super.onResume();
	}
	
	@Override
	protected void onPause() {
		MyCity.pause();
	    super.onPause();
	}

	@Override
	public void onClick(View v) {
		if (!XMPPController.isLoggedIn()) {
			EditText editUsername = (EditText) findViewById(R.id.loginEmail);
			EditText editPassword = (EditText) findViewById(R.id.loginPassword);
			String username = editUsername.getText().toString();
			String password = editPassword.getText().toString();
			XMPPController.connectToGtalk(username, password); //XMPP Controller will start use the ViewController to change to the next screen
		} else {
			ViewController.doLoginIntent();
		}
	}
}
